package com.btex.app.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.btex.app.MyApplication;
import com.btex.app.R;
import com.btex.app.entity.NewBiInfo;
import com.btex.app.tools.SharedPreferencesUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/5/17
 * @time 下午7:17
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class HomePageAdapter extends BaseQuickAdapter<NewBiInfo.DataBean, BaseViewHolder> {

    public HomePageAdapter(@Nullable List<NewBiInfo.DataBean> data) {
        super(R.layout.item_shou_ye_recy, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, NewBiInfo.DataBean item) {
        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(MyApplication.getContextObject());
        helper.setText(R.id.item_news_type, item.getType() + "/" + item.getDanwei());

        if (item.getTicker().getTrade_price().length() > 10) {
            helper.setText(R.id.item_news_price, item.getTicker().getTrade_price().substring(0, 10));
        } else {
            helper.setText(R.id.item_news_price, item.getTicker().getTrade_price());
        }


        boolean isEngilsh = sharedPreferences.getBoolean("isEngilsh", false);
        if (isEngilsh) {
            helper.setText(R.id.item_news_money, "$" + item.getTicker().getRate_price_u());

        } else {
            helper.setText(R.id.item_news_money, "￥" + item.getTicker().getRate_price_c());
        }

        String change_24h = item.getTicker().getChange_24h();
        String replace = change_24h.replace(",", "");

        if (!TextUtils.isEmpty(replace)) {

            Double aDouble = Double.parseDouble(replace);
            double v = aDouble * 100;


            helper.setTextColor(R.id.item_news_zhang_die, MyApplication.getContextObject().getResources().getColor(R.color.white));
            if (aDouble > 0) {

                helper.setBackgroundRes(R.id.item_news_zhang_die, R.drawable.item_bg_no);
                String s = String.valueOf(v);
                if (s.contains(".")) {

                    String[] split = s.split("\\.");
                    String s1 = split[1];
                    String s0 = split[0];


                    if (s1.length() > 2) {

                        String substring = s1.substring(0, 2);
                        String s2 = s0 + "." + substring;

                        helper.setText(R.id.item_news_zhang_die, "+" + s2 + "%");
                    } else {

                        String s2 = s0 + "." + s1;
                        helper.setText(R.id.item_news_zhang_die, "+" + s2 + "%");
                    }

                } else {

                    helper.setText(R.id.item_news_zhang_die, "+" + s + "%");
                }

//                    int length = String.valueOf(v).length();
//                    if (length > 4) {
//                        helper.setText(R.id.item_trade_zhang_die, "+" + String.valueOf(v).substring(0, 4) + "%");
//                    } else {
//                        helper.setText(R.id.item_trade_zhang_die, "+" + String.valueOf(v) + "%");
//                    }

            } else if (aDouble == 0) {
                String s = String.valueOf(v);
                helper.setBackgroundRes(R.id.item_news_zhang_die, R.drawable.item_bg_no);
                helper.setText(R.id.item_news_zhang_die, "+" + s + "%");
            } else {

                helper.setBackgroundRes(R.id.item_news_zhang_die, R.drawable.item_bg);
                String s = String.valueOf(v);
                if (s.contains(".")) {

                    String[] split = s.split("\\.");
                    String s1 = split[1];
                    String s0 = split[0];
                    if (s1.length() > 2) {

                        String substring = s1.substring(0, 2);
                        String s2 = s0 + "." + substring;
                        helper.setText(R.id.item_news_zhang_die, s2 + "%");
                    } else {

                        String s2 = s0 + "." + s1;
                        helper.setText(R.id.item_news_zhang_die, s2 + "%");
                    }

                } else {

                    helper.setText(R.id.item_news_zhang_die, s + "%");
                }

//                    int length = String.valueOf(v).length();
//                    if (length > 4) {
//                        helper.setText(R.id.item_trade_zhang_die, String.valueOf(v).substring(0, 5) + "%");
//                    } else {
//                        helper.setText(R.id.item_trade_zhang_die, String.valueOf(v) + "%");
//                    }
            }
        }

    }
}
